import requests
import time

def generate_test_messages():
    """生成测试消息"""
    print("=== 生成测试消息 ===")
    
    # 等待服务启动
    print("等待服务启动...")
    time.sleep(5)
    
    try:
        # 生成测试消息
        response = requests.post('http://localhost:8080/api/test/generate-messages', timeout=30)
        if response.ok:
            print(f"✓ {response.text}")
        else:
            print(f"✗ 生成失败: {response.status_code} - {response.text}")
            return
            
        # 验证消息是否生成成功
        time.sleep(2)
        
        # 获取任务列表
        tasks_response = requests.get('http://localhost:8080/api/tasks')
        if not tasks_response.ok:
            print("✗ 无法获取任务列表")
            return
            
        tasks = tasks_response.json()
        if not tasks:
            print("✗ 没有任务数据")
            return
            
        task_id = tasks[0]['id']
        
        # 获取最新的日志
        logs_response = requests.get(f'http://localhost:8080/api/logs/{task_id}?page=0&size=5')
        if logs_response.ok:
            logs_data = logs_response.json()
            print(f"✓ 当前共有 {logs_data.get('totalElements', 0)} 条日志")
            
            if logs_data.get('content'):
                # 测试最新消息的详情
                latest_message = logs_data['content'][0]
                message_id = latest_message['id']
                
                detail_response = requests.get(f'http://localhost:8080/api/logs/message/{message_id}')
                if detail_response.ok:
                    detail_data = detail_response.json()
                    content = detail_data.get('fullMessageContent', '')
                    print(f"✓ 最新消息内容长度: {len(content)}")
                    print(f"✓ 消息预览: {content[:100]}...")
                    
                    if len(content) > 10 and content != '消息内容为空':
                        print("✓ 测试消息生成成功，包含完整内容！")
                    else:
                        print("⚠️ 消息内容仍然为空或过短")
                else:
                    print(f"✗ 获取消息详情失败: {detail_response.status_code}")
        else:
            print(f"✗ 获取日志失败: {logs_response.status_code}")
            
    except Exception as e:
        print(f"✗ 操作失败: {e}")

if __name__ == "__main__":
    generate_test_messages()
